package net.fxug.hokuriku.chocolat.dao.contents
{
    import flash.data.SQLConnection;
    import flash.data.SQLResult;
    import flash.data.SQLStatement;
    
    import net.fxug.hokuriku.chocolat.dao.SessionDao;
    import net.fxug.hokuriku.chocolat.dao.VoteDao;
    
    import org.flexunit.Assert;
    import org.flexunit.assertThat;
    import org.hamcrest.core.isA;

	/**
	 * SQLQueryのテストクラス。
	 * 面倒なので非同期ではなく同期的にテストを行う。
	 * なお、起動するDBはインメモリモードで起動するので、ゴミファイルなどは残らない。
	 */
    public class SQLQueryTest
    {

		private var con:SQLConnection;

		public function SQLQueryTest()
        {
        }
		[Before]
		public function beforeClass():void
		{
			con = new SQLConnection();
			try {
//				trace(File.desktopDirectory.resolvePath("rrr.db"));
//				con.open(File.desktopDirectory.resolvePath("rrr.db"));
				con.open(null);
			} catch (error1:Error) {
				Assert.fail(error1.getStackTrace());
			}
			
			var sqlStmt:SQLStatement = new SQLStatement();
			sqlStmt.sqlConnection = con;
			sqlStmt.text = SQLQuery.CREATE_TABLE_SESSION;
			try {
				trace(sqlStmt.text);
				sqlStmt.execute();
			} catch (error:Error)
			{
				Assert.fail("DBの生成に失敗しました");
			}
			sqlStmt.text = SQLQuery.CREATE_TABLE_VOTE;
			try {
				trace(sqlStmt.text);
				sqlStmt.execute();
			} catch (error:Error)
			{
				Assert.fail("DBの生成に失敗しました");
			}
		}
		[After]
		public function afterClass():void
		{
		}

		[Test]
		public function testInsertSelectVote():void
		{
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = con;
//			stmt.itemClass = VoteDao;
			var voteDao:VoteDao = new VoteDao();
			voteDao.sessionId = 1;
			voteDao.datetime = new Date();
			voteDao.userId = "11";
			voteDao.tweet = "hogehoge";
			voteDao.protocol = true;
			voteDao.route = "twitter";

			stmt.text = SQLQuery.INSERT_VOTE;

			stmt.parameters[":sessionId"] = voteDao.sessionId;
			stmt.parameters[":datetime"] = voteDao.datetime;
			stmt.parameters[":userId"] = voteDao.userId;
			stmt.parameters[":tweet"] = voteDao.tweet;
			stmt.parameters[":protocol"] = voteDao.protocol;
			stmt.parameters[":route"] = voteDao.route;


			try {
				// insert
				stmt.execute();
				stmt.getResult();
			} catch(e2:Error)
			{
				Assert.fail(e2.getStackTrace());
			}
			try {
				var stmt2:SQLStatement = new SQLStatement();
				stmt2.sqlConnection = con;
				stmt2.text = SQLQuery.SELECT_VOTE;
				stmt2.itemClass = VoteDao;
				stmt2.execute();
	
				var result:SQLResult = stmt2.getResult();
				assertThat(result.data.length, isA(1));

				var selectResult:VoteDao = result.data[0] as VoteDao;
				assertThat(selectResult.sessionId, isA(1));
				assertThat(selectResult.protocol, isA(true));
				assertThat(selectResult.datetime.time, isA(voteDao.datetime.time));
				assertThat(selectResult.tweet, isA(voteDao.tweet));
				assertThat(selectResult.userId, isA(voteDao.userId));

			} catch (e:Error) {
				Assert.fail(e.getStackTrace());
			}
		}
		[Test]
		public function testSession():void
		{
//			var d:Date = new Date();
			var d:Date = new Date(2001,10,11,10,15,11);
			
			// insert
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = con;
			stmt.text = SQLQuery.INSERT_SESSION;
//			stmt.parameters[":sessionId"] = 1;
			stmt.parameters[":startDatetime"] = d;
			stmt.parameters[":title"] = "#hoge";
			stmt.parameters[":sessionTag"] = "$piyo";
			stmt.parameters[":atndId"] = 102;
			try {
				trace(stmt.text);
				stmt.execute();
			} catch (e:Error) {
				Assert.fail(e.getStackTrace());
			}
			
			// select
			var stmt2:SQLStatement = new SQLStatement();
			stmt2.sqlConnection = con;
			stmt2.text = SQLQuery.SELECT_SESSION;
			stmt2.itemClass = SessionDao;
			try {
				trace(stmt2.text);
				stmt2.execute();
				var ary:Array = stmt2.getResult().data;
				assertThat(ary.length, isA(1));
				
				var dao:SessionDao = ary[0] as SessionDao;
				assertThat(dao.atndId, isA(102));
				assertThat(dao.sessionTag, isA("$piyo"));
				trace("1:[" + dao.startDatetime + "]" + dao.startDatetime.time + "\n2:[" + d + "]" + d.time);
//				assertThat(dao.startDatetime.getTime(), isA(d.getTime()));
				Assert.assertEquals(dao.startDatetime.time, d.time);
				assertThat(dao.title, isA("#hoge"));
				assertThat(dao.sessionId, isA(1));
			} catch (e2:Error)
			{
				Assert.fail(e2.getStackTrace());
			}
			
		}
    }
}